package com.vae.learn.service.impl;

import com.vae.base.model.video.RestResponse;
import com.vae.content.domain.entity.CoursePublish;
import com.vae.dto.XcCourseTablesDto;
import com.vae.learn.feignclient.ContentServiceClient;
import com.vae.learn.feignclient.MediaServiceClient;
import com.vae.learn.service.LearningService;
import com.vae.learn.service.MyCourseTablesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 在线学习接口
 */
@Service
public class LearningServiceImpl implements LearningService {

    @Autowired
    MyCourseTablesService myCourseTablesService;

    @Autowired
    ContentServiceClient contentServiceClient;

    @Autowired
    MediaServiceClient mediaServiceClient;

    /**
     * 在线学习，获取视频播放地址
     * @param userId
     * @param courseId    课程id
     * @param teachplanId 课程计划id
     * @param mediaId     视频文件id
     * @return
     */
    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        //判断如果为null不再继续
        if(coursepublish == null){
            return RestResponse.validfail("课程不存在");
        }

        //远程调用内容管理服务根据课程计划id（teachplanId）去查询课程计划信息，如果is_preview的值为1表示支持试学
        //也可以从coursepublish对象中解析出课程计划信息去判断是否支持试学
        Map<Integer, String> teachplanMap = contentServiceClient.getTeachplanByCourseId(courseId);

        //用户已登录
        if(StringUtils.isNotEmpty(userId) || userId==null){
            //获取学习资格
            XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
            //学习资格，[{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
            String learnStatus = learningStatus.getLearnStatus();
            if("702003".equals(learnStatus)){
                return RestResponse.validfail("已过期需要申请续期或重新支付");
            }else{
                Integer correspondingKey = null;
                for (Map.Entry<Integer, String> entry : teachplanMap.entrySet()) {
                    if (entry.getValue().equals(mediaId)) {
                        correspondingKey = entry.getKey();
                        break;
                    }
                }
                if (correspondingKey != null) {
                    Boolean isPreview = contentServiceClient.getTeachplanIsPreview(Long.valueOf(correspondingKey));
                    RestResponse<String> playUrlByMediaId;
                    //可以试学 || 已支付 || 免费课程。这三种情况的话，就返回视频地址
                    if (isPreview || "702001".equals(learnStatus) || "201000".equals(coursepublish.getCharge())) {
                        playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                        return playUrlByMediaId;
                    } else {
                        return RestResponse.validfail("此视频不支持预览/试学，点击右侧目录试试其它视频吧");
                    }
                }
            }

        }
        return RestResponse.validfail("课程视频的信息异常");
    }
}